//
//  FMSqliteTool.m
//  面向对象的数据库操作
//
//  Created by appbanana on 2017/3/18.
//  Copyright © 2017年 appbanana. All rights reserved.
//

#import "FMSqliteTool.h"
#import "sqlite3.h"

#define kCache   NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject

@implementation FMSqliteTool
sqlite3 *ppDb;

+ (BOOL)dealSql:(NSString *)sql uid:(NSString *)uid{
    //打开数据库并判断
    if (![self openDbWithUid:uid]) {
        NSLog(@"打开数据库失败");
        return NO;
    }
    BOOL result = sqlite3_exec(ppDb, sql.UTF8String, nil, nil, nil) == SQLITE_OK;
    //关闭数据库
    [self closeDb];
    
    return result;
}


+ (BOOL)dealSqls:(NSArray *)sqls uid:(NSString *)uid{
    //打开数据
    if (![self openDbWithUid:uid]) {
        NSLog(@"打开数据库失败");
        return NO;
    }
    //开启事务
    sqlite3_exec(ppDb, @"begin transaction;".UTF8String, nil, nil, nil);
    for (NSString *sql in sqls) {
        //如果有错误 回滚
        BOOL result = sqlite3_exec(ppDb, sql.UTF8String, nil, nil, nil) == SQLITE_OK;
        if (result == NO) {
            sqlite3_exec(ppDb, @"rollback transaction;".UTF8String, nil, nil, nil);
            return NO;
        }
    }
    sqlite3_exec(ppDb, @"commit transaction;".UTF8String, nil, nil, nil);
    //关闭数据库
    [self closeDb];
    return YES;
}


+ (NSArray<NSDictionary *> *)querySql:(NSString *)sql uid:(NSString *)uid{
    //打开数据库
    [self openDbWithUid:uid];
    
    //预处理
    sqlite3_stmt *ppStmt;
    sqlite3_prepare_v2(ppDb, sql.UTF8String, -1, &ppStmt, nil);
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
    while (sqlite3_step(ppStmt) == SQLITE_ROW) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
        [array addObject:dic];
       int count = sqlite3_column_count(ppStmt);
        for (int i = 0; i < count; i++) {
           const char *cName = sqlite3_column_name(ppStmt, i);
            NSString *columnName = [NSString stringWithUTF8String:cName];
           int type = sqlite3_column_type(ppStmt, i);
            id value;
            switch (type) {
                case SQLITE_INTEGER:
                    value = @(sqlite3_column_int(ppStmt, i));
                    break;
                case SQLITE_FLOAT:
                    value = @(sqlite3_column_double(ppStmt, i));
                    break;
                case SQLITE_BLOB:{
                    const char *cValue = sqlite3_column_blob(ppStmt, i);
                    value = [NSString stringWithUTF8String:cValue];
                    break;
                }
                case SQLITE_NULL:
                    value = @"";
                    break;
                case SQLITE3_TEXT:{
                    const char *cText = (const char *)sqlite3_column_text(ppStmt, i);
                    value = [NSString stringWithUTF8String:cText];
                }
                    break;
                default:
                    break;
            }
            [dic setValue:value forKey:columnName];
        }
    }
    sqlite3_finalize(ppStmt);
    [self closeDb];

    return array;
}


+ (BOOL)openDbWithUid:(NSString *)uid{
//    NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)
    NSString *filePath = [kCache stringByAppendingPathComponent:@"common.sqlite"];
    if (uid.length > 0) {
        filePath = [NSString stringWithFormat:@"%@.sqlite", uid];
    }
    BOOL result = sqlite3_open(filePath.UTF8String, &ppDb) == SQLITE_OK;
    if (result) {
        NSLog(@"数据库创建成功 并打开");
    }else{
        NSLog(@"打开数据库失败");
    }
    return result;
}

+ (void)closeDb{
    sqlite3_close(ppDb);
}

@end
